package com.sylar.happyreader.net;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.ByteArrayBuffer;

import com.sylar.happyreader.tools.Utils;

import android.content.Context;

/**
 * @author lixiurui
 * 上层封装的Http工具类
 */
public class HttpTool {
//	private static final String tag=FtHttp.class.getSimpleName();
	
	/**
	 * 访问某个http地址，获取其返回的内容
	 * @param context
	 * @param contentUrl http地址
	 * @return 返回获取到的内容，可能为null
	 */
	public static final byte[] getData(Context context,String contentUrl)
	{
		byte[] data=null;
		HttpGet get=HttpHelper.get(contentUrl);
		if(get!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,get);
			if(resp!=null)
			{
				data=HttpHelper.parserHttpResponseToByte(resp);
			}
		}		
		HttpHelper.abortRequest(get);
		return data;
	}
	
	/**
	 * 访问一个http地址，获取其返回的状态码和数据
	 * @param context
	 * @param returnData byte数组缓存，获取到的数据将存放到该缓存中
	 * @param contentUrl http地址
	 * @return 返回http状态码，如果为0则表明没有执行或者返回空或者网络错误
	 */
	public static final int getData(Context context,ByteArrayBuffer returnData,String contentUrl)
	{
		HttpGet get=HttpHelper.get(contentUrl);
		int respCode=0;
		if(get!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,get);
			if(resp!=null)
			{
				respCode=resp.getStatusLine().getStatusCode();
				if(returnData!=null)
				{
					byte[] data=HttpHelper.parserHttpResponseToByte(resp);
					returnData.append(data, 0, data.length);
				}				
			}
		}		
		HttpHelper.abortRequest(get);
		return respCode;
	}

	
	/**
	 * 通过get方法访问一个http地址，获取其返回的字符串内容
	 * @param context
	 * @param contentUrl http地址
	 * @return 返回获取到的字符串，可能为null
	 */
	public static final String getString(Context context,String contentUrl)
	{
		Utils.LogD("request url : " + contentUrl);
		String data=null;
		HttpGet get=HttpHelper.get(contentUrl);
		if(get!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,get);
			if(resp!=null)
			{
				
				data=HttpHelper.parserHttpResponseToString(resp);
			}
		}		
		HttpHelper.abortRequest(get);
		return data;
	}
	
	public static final InputStream getInputStream(Context context,String contentUrl)
	{
		InputStream is = null;
//		String data=null;
		HttpGet get=HttpHelper.get(contentUrl);
		if(get!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,get);
			if(resp!=null)
			{
				try {
					is = resp.getEntity().getContent();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				data=HttpHelper.parserHttpResponseToString(resp);
			}
		}		
		HttpHelper.abortRequest(get);
		return is;
	}
	
	/**
	 * 通过post方法访问一个http地址，获取其返回的字符串内容
	 * @param context
	 * @param httpUrl http地址
	 * @param postData post的数据
	 * @return 返回获取到的字符串，可能为null
	 */
	public static final String getPostString(Context context,String httpUrl,String postData)
	{
		String data=null;
		HttpPost post=HttpHelper.post(httpUrl, postData);
		if(post!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,post);
			if(resp!=null)
			{
				data=HttpHelper.parserHttpResponseToString(resp);
			}
		}		
		HttpHelper.abortRequest(post);
		return data;
	}
	
	
	public static final String getPostXml(Context context,String httpUrl,String postData)
	{
		String data=null;
		HttpPost post=HttpHelper.postXml(httpUrl, postData);
		if(post!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,post);
			if(resp!=null)
			{
				data=HttpHelper.parserHttpResponseToString(resp);
			}
		}		
		HttpHelper.abortRequest(post);
		return data;
	}
	
	/**
	 * 通过post方法访问一个http地址，获取其返回的http状态码和字符串内容
	 * @param context
	 * @param returnData byte数组缓存，获取到的数据将存放到该缓存中
	 * @param httpUrl http地址
	 * @param postData post的数据
	 * @return 返回http状态码，如果为0则表明没有执行或者返回空或者网络错误
	 */
	public static final int postString(Context context,ByteArrayBuffer returnData,String httpUrl,String postData)
	{
		int respCode=0;
		HttpPost post=HttpHelper.post(httpUrl, postData);
		if(post!=null)
		{
			HttpResponse resp=HttpHelper.processHttpRequest(context,post);
			if(resp!=null)
			{
				respCode=resp.getStatusLine().getStatusCode();
				if(returnData!=null)
				{
					byte[] data=HttpHelper.parserHttpResponseToByte(resp);
					if(data!=null)returnData.append(data, 0, data.length);
				}
			}
		}		
		HttpHelper.abortRequest(post);
		return respCode;
	}
}
